package service;

import mapper.account.ManagerMapper;
import mapper.account.ReaderMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.account.Manager;
import pojo.account.Reader;
import util.DBUtil;

import java.io.IOException;
import java.util.List;

public class AccountService {
    public Reader getReader(String id) {
        Reader reader = null;
        try {
            SqlSession sqlSession = DBUtil.getSqlSession();
            ReaderMapper mapper = sqlSession.getMapper(ReaderMapper.class);
            reader = mapper.query(id);
            sqlSession.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return reader;
    }

    public List<Reader> getAllReader() {
        List<Reader> readerList = null;
        try {
            SqlSession sqlSession = DBUtil.getSqlSession();
            ReaderMapper mapper = sqlSession.getMapper(ReaderMapper.class);
            readerList = mapper.queryAll();
            sqlSession.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return readerList;
    }

    public List<Reader> getAllReaderByID(String id) {
        List<Reader> readerList = null;
        try {
            SqlSession sqlSession = DBUtil.getSqlSession();
            ReaderMapper mapper = sqlSession.getMapper(ReaderMapper.class);
            readerList = mapper.queryAllById(id);
            sqlSession.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return readerList;
    }

    public List<Reader> getAllReaderByName(String name) {
        List<Reader> readerList = null;
        try {
            SqlSession sqlSession = DBUtil.getSqlSession();
            ReaderMapper mapper = sqlSession.getMapper(ReaderMapper.class);
            readerList = mapper.queryAllByName(name);
            sqlSession.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return readerList;
    }

    public Manager getManager(String id) {
        Manager manager = null;
        try {
            SqlSession sqlSession = DBUtil.getSqlSession();
            ManagerMapper mapper = sqlSession.getMapper(ManagerMapper.class);
            manager = mapper.query(id);
            sqlSession.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return manager;
    }
}
